import { createRouter, createWebHashHistory} from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout } from '@/utils/routerHelper'

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/page',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'RedirectComponent',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },{
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: "登录",
      noTagsView: true
    }
  },
        {
          path: '/device/member',
          component: ()=>import('@/views/DevicePage/Member/Index.vue'),
          name: 'memberList',
          meta: {
            title: '会员档案',
            icon: 'carbon:home',
            hidden: true,
            noTagsView: true
          }
        },{
          path: '/device/teacher',
          component: ()=>import('@/views/DevicePage/Teacher/Index.vue'),
          name: 'teacher',
          meta: {
            title: '选择老师',
            icon: 'carbon:home',
            hidden: true,
            noTagsView: true
          }
        },{
          path: '/device/sensory',
          component: ()=>import('@/views/DevicePage/Sensory/Index.vue'),
          name: 'sensory',
          meta: {
            title: '感统评测',
            icon: 'carbon:home',
            hidden: true,
            noTagsView: true
          }
        },{
          path: '/device/sensoryReport',
          component: ()=>import('@/views/DevicePage/Sensory/Report.vue'),
          name: 'sensoryReport',
          meta: {
            title: '感统评测报告',
            icon: 'carbon:home',
            hidden: true,
            noTagsView: true
          }
        },
        {
          path: '/device/testList',
          component: ()=>import('@/views/DevicePage/Test/Index.vue'),
          name: 'testList',
          meta: {
            title: '评测结果',
            icon: 'carbon:home',
            hidden: true,
            noTagsView: true
          }
        },{
          path: '/device/report',
          component: ()=>import('@/views/DevicePage/Report/Index.vue'),
          name: 'report',
          meta: {
            title: '评测报告',
            icon: 'carbon:home',
            hidden: true,
            noTagsView: true
          }
        },{
          path: '/device/analysis',
          component: ()=>import('@/views/DevicePage/Analysis/Index.vue'),
          name: 'analysis',
          meta: {
            title: '统计分析',
            icon: 'carbon:home',
            hidden: true,
            noTagsView: true
          }
        },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncPageRouterMap: AppRouteRecordRaw[] = []
const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
